package com.ac.config;

import com.ac.interceptor.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private LoginInterceptor loginInterceptor;
 @Override
   public void addInterceptors(InterceptorRegistry registry){
     //登录注册主页放行
     registry.addInterceptor(loginInterceptor).excludePathPatterns("/acUser/wxLogin","/acUser/add","/acUser/login","/acUser/resetPwd","/acUser/logout","/webjars/springfox-swagger-ui/fonts/titillium-web-v6-latin-regular" +
                     ".woff2",
             "/webjars/springfox-swagger-ui/fonts/source-code-pro-v7-latin-600.woff2", "/csrf", "/webjars" +
                     "/springfox-swagger-ui/fonts/titillium-web-v6-latin-600.woff2",
             "/webjars/springfox-swagger-ui/fonts/source-code-pro-v7-latin-300.woff2", "/webjars/springfox-swagger" +
                     "-ui/fonts/open-sans-v15-latin-700.woff2",
             "/webjars/springfox-swagger-ui/fonts/titillium-web-v6-latin-700.woff2", "/webjars/springfox-swagger" +
                     "-ui/fonts/open-sans-v15-latin-regular.woff2", "/",
             "/swagger-resources", "/swagger-resources/configuration/security", "/swagger-resources/configuration" +
                     "/ui", "/webjars/springfox-swagger-ui/favicon-16x16.png",
             "/webjars/springfox-swagger-ui/springfox.js", "/webjars/springfox-swagger-ui/swagger-ui-bundle.js",
             "/webjars/springfox-swagger-ui/springfox.css",
             "/webjars/springfox-swagger-ui/swagger-ui-standalone-preset.js", "/webjars/springfox-swagger-ui" +
                     "/swagger-ui.css", "/favicon.ico", "/error", "/swagger-ui.html",
             "/swagger-resources/**", "/webjars/**", "static/**");
 }

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();

// 允许所有域名（生产环境建议指定具体域名）
        config.addAllowedOrigin("*");

// 允许的请求头
        config.addAllowedHeader("*");

// 允许的HTTP方法
        config.addAllowedMethod("*");

// 允许跨域发送Cookie（如果要使用，必须指定具体域名，不能是"*"）
// config.setAllowCredentials(true);

        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }
}
